package com.ghl.crawler;

import com.ghl.crawler.core.DBHelper;
import com.ghl.crawler.entity.Item;
import com.ghl.crawler.entity.Picture;
import com.ghl.crawler.mapper.ItemMapper;
import com.ghl.crawler.mapper.PictureMapper;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description
 * @Author haiLong <1105744725@qq.com>
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2019/11/24 10:39
 */
public class TestJsoup {

    /**
     * http://www.bytravel.cn/landscape/105/weijiahutong18haozhaiyuan.html
     * <p>
     * http://www.bytravel.cn/landscape/105/qiangongyonghutongshiwuhaosiheyuan.html
     * <p>
     * http://www.bytravel.cn/landscape/110/beijingbadalingmutianyuchangchenglvyouqu.html
     */

    private static String url1 = "http://www.bytravel.cn/landscape/105/weijiahutong18haozhaiyuan.html";

    private static String url2 = "http://www.bytravel.cn/landscape/105/qiangongyonghutongshiwuhaosiheyuan.html";

    private static String url3 = "http://www.bytravel.cn/landscape/110/beijingbadalingmutianyuchangchenglvyouqu.html";

    private static final String[] USERAGENT = {
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
            "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1", "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
            "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
    };

    public static void main(String[] args) throws Exception {

//        Document document = Jsoup.connect(url3).header("User-Agent", USERAGENT[(int) Math.random() * 5]).get();
//        Elements imgElements = document.getElementById("page_left").select("img");
//        System.out.println(imgElements.get(0).attr("abs:src").trim());
//
//        imgElements.forEach(item -> {
//            String url = item.attr("abs:src").trim();
//            if (!url.contains("http://www.bytravel.cn")){
//                System.out.println(item.attr("abs:src").trim());
//            }
//        });
        execute();

    }


    public static void execute() {

        SqlSession session = DBHelper.getSession();
        ItemMapper itemMapper = session.getMapper(ItemMapper.class);
        PictureMapper pictureMapper = session.getMapper(PictureMapper.class);
        List<Item> items = itemMapper.listQuery(null);
        ExecutorService executorService = Executors.newFixedThreadPool(20);

        for (Item item : items) {

            Item preItem = item;
            if (preItem.getId() <= 6546) {
                continue;
            }

//            System.out.println(item);
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //源网址
                    String url = preItem.getSource();
                    Document document = null;
                    try {
                        document = Jsoup.connect(url).header("User-Agent", USERAGENT[(int) Math.random() * 5]).get();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Elements imgElements = document.getElementById("page_left").select("img");
//                    System.out.println(imgElements.get(0).attr("abs:src").trim());
                    List<Picture> pictureList = new ArrayList<Picture>();
                    imgElements.forEach(item -> {
                        String imgUrl = item.attr("abs:src").trim();
                        if (!imgUrl.contains("http://www.bytravel.cn")) {
                            //再次新建记录.
                            Picture picture = new Picture();
                            picture.setItemId(preItem.getId());
                            picture.setImgSource(imgUrl);
                            picture.setCreateTime(new Date());
                            pictureList.add(picture);
                        }
                    });
                    if (pictureList.size() > 0) {
                        pictureMapper.insertBatch(pictureList);
                    }

                }
            });
        }
        session.close();
        executorService.shutdown();

    }


}
